package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.model.SpuInfo;
import com.atguigu.gmall.product.model.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-08-30 14:28
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private SpuManageService spuManageService;

    /**
     * 分页查询商品SPU列表
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("分页查询商品SPU列表")
    @GetMapping("/{page}/{size}")
    public Result getSpuByPage(@PathVariable("page") int page, @PathVariable("size") int size, @RequestParam("category3Id") Long category3Id) {
        Page<SpuInfo> spuInfoPage = new Page<>(page, size);
        spuInfoPage = spuManageService.getSpuByPage(spuInfoPage, category3Id);
        return Result.ok(spuInfoPage);
    }


    /**
     * 商品SPU保存
     * @param spuInfo
     * @return
     */
    @ApiOperation("商品SPU保存")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据spuId 获取spu图片集合
     * @param spuId
     * @return
     */
    @ApiOperation("根据spuId 获取spu图片集合")
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuManageService.getSpuImageList(spuId);
        return Result.ok(list);
    }

    /**
     * 根据spuId 查询销售属性
     * @param spuId
     * @return
     */
    @ApiOperation("根据spuId 查询销售属性")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }
}
